package com.example.sonone.error;


import com.fasterxml.jackson.annotation.JsonIgnore;


/**
 * 用于向前端返回数据
 */
public class ServerResponse {
	private int status; // 请求的状态
	private String msg; // 请求的状态描述
	private Object data; // 请求的数据

	private ServerResponse(int status) {
		this.status = status;
	}

	private ServerResponse(int status, Object data) {
		this.status = status;
		this.data = data;
	}

	private ServerResponse(int status, String msg) {
		this.status = status;
		this.msg = msg;
	}

	private ServerResponse(int status, String msg, Object data) {
		this.status = status;
		this.msg = msg;
		this.data = data;
	}

	@JsonIgnore
	public boolean isSuccess() {
		return this.status == TranspunitConstant.SUCCESS;
	}

	public int getStatus() {
		return status;
	}

	public Object getData() {
		return data;
	}

	public String getMsg() {
		return msg;
	}

	/** 所有情况  */
	public static ServerResponse createSuccessTrue(int status, String msg, Object data) {
		return new ServerResponse(status, msg, data);
	}
	
	/** 查询操作 */
	public static ServerResponse createSuccessTrue(String msg, Object data) {
		return new ServerResponse(TranspunitConstant.SUCCESS, msg, data);
	}

	/** 增删改操作成功 */
	public static ServerResponse createSuccessTrue(String msg) {
		return new ServerResponse(TranspunitConstant.SUCCESS, msg);
	}
	
	/**
	 * 特殊情况，操作token失效等
	 * @param status
	 * @param msg
	 * @return
	 */
	public static ServerResponse createErrorHandle(int status, String msg) {
		return new ServerResponse(status, msg);
	}

	/** 增删改操作失败 */
	public static ServerResponse createErrorHandle(String msg) {
		return new ServerResponse(TranspunitConstant.ERROR, msg);
	}

	/** 异常 */
	public static ServerResponse createErrorException(int status, String msg) {
		return new ServerResponse(status, msg);
	}

	/** 参数校验 */
	public static ServerResponse createErrorParam(String msg) {
		return new ServerResponse(TranspunitConstant.ERROR, msg);
	}

	public void setStatus(int status) {
		this.status = status;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}
}
